import Vue from 'vue'
import App from './App.vue'
import router from "./router"
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueLazyLoad from 'vue-lazyload'
import VuejsDialog from "vuejs-dialog"
import "vuejs-dialog/dist/vuejs-dialog.min.css"

Vue.use(VuejsDialog)
Vue.use(VueAxios, axios);
Vue.use(VueLazyLoad, {
    error: require('./assets/images/h5_false.png'),
    loading: require('./assets/images/h5_loading.png')
});

Vue.config.productionTip = false;

Vue.prototype.convertDateFromString = function (dateString) {
    if (dateString) {
        var d = new Date(Date.parse(dateString.replace(/-/g, "/")));
        return d
    }
}

function IsPC()
{
    var userAgentInfo = navigator.userAgent;
    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }
    }

    return flag;
}

// 全局函数部分
Vue.prototype.toRemarkTime=function(time){
    let dstr="";
    if (time != null || time != "" || typeof (time) != "undefined") {
        let year;
        let month;
        let day;
        let hour;
        let minute;
        var date = new Date(time * 1000);
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        hour = date.getHours();
        minute = date.getMinutes();

        if (month < 10) month = "0" + month;
        if (day < 10) day = "0" + day;
        if (hour < 10) hour = "0" + hour;
        if (minute < 10) minute = "0" + minute;
        let timeStr = year + "-" + month + "-" + day + " " + hour + ":" + minute;
        let dates = this.convertDateFromString(timeStr);
        if (day == new Date().getDate()) {
            dstr = hour + ":" + minute;
        } else if (year == new Date().getFullYear()) {
            dstr = month + "-" + day + " " + hour + ":" + minute;

        } else {
            dstr = year + "-" + month + "-" + day;
        }
    }
    return dstr;
}

/**
 * 自定义指令 v-reset-page，以解决 iOS 12 中键盘收起后页面底部有留白的问题
 */

Vue.directive('resetPage', {
    inserted: function (el) {
        // 监听键盘收起事件
        document.body.addEventListener('focusout', () => {
            if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                //软键盘收起的事件处理
                setTimeout(() => {
                    const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0
                    window.scrollTo(0, Math.max(scrollHeight - 1, 0))
                }, 100)
            }
        })
    }
})


Vue.prototype.toCommonTime = function (time) {
    if (time != null || time != "" || typeof (time) != "undefined") {
        let year;
        let month;
        let day;
        let hour;
        let minute;
        let dstr;
        if (/^\d{10}$/.test(time)) {
            // int 时间戳类型
            var date = new Date(time * 1000);
            year = date.getFullYear();
            month = date.getMonth() + 1;
            day = date.getDate();
            hour = date.getHours();
            minute = date.getMinutes();

            if (month < 10) month = "0" + month;
            if (day < 10) day = "0" + day;
            if (hour < 10) hour = "0" + hour;
            if (minute < 10) minute = "0" + minute;
        } else if (/^[0-9]{14}$/.test(time)) {
            // string 日期时间类型
            // console.log(time);
            year = time.substr(0, 4);
            month = time.substr(4, 2);
            day = time.substr(6, 2);
            hour = time.substr(8, 2);
            minute = time.substr(10, 2);
        }
        let timeStr = year + "-" + month + "-" + day + " " + hour + ":" + minute;
        console.log(timeStr)
        let dates = this.convertDateFromString(timeStr);
        console.log(dates)
        let subt = parseInt((new Date() - dates) / 1000);
        if (subt / (60) <= 5) {
            dstr = "刚刚";
        } else if (subt / (60) <= 180) {
            let hh = parseInt(subt / (60 * 60));
            let mm = parseInt((subt - hh * 60 * 60) / 60);
            dstr = ((hh == 0) ? "" : (hh + "小时")) + ((mm == 0) ? "" : (mm + "分钟")) + " 前";
        } else if (day == new Date().getDate()&&month==(new Date().getMonth() + 1)&&year==new Date().getFullYear()) {
            dstr = "今天";
        } else if (year == new Date().getFullYear()) {
            dstr = month + "-" + day + " " + hour + ":" + minute;
           
        } else {
            dstr = year + "-" + month + "-" + day;
        }

        return dstr;

    }
};

Vue.prototype.toFullCommonTime=function(time){
    if (time != null || time != "" || typeof (time) != "undefined") {
        let year;
        let month;
        let day;
        let hour;
        let minute;
        let dstr;
        if (/^\d{10}$/.test(time)) {
            // int 时间戳类型
            var date = new Date(time * 1000);
            year = date.getFullYear();
            month = date.getMonth() + 1;
            day = date.getDate();
            hour = date.getHours();
            minute = date.getMinutes();

            if (month < 10) month = "0" + month;
            if (day < 10) day = "0" + day;
            if (hour < 10) hour = "0" + hour;
            if (minute < 10) minute = "0" + minute;
        } else if (/^[0-9]{14}$/.test(time)) {
            // string 日期时间类型
            // console.log(time);
            year = time.substr(0, 4);
            month = time.substr(4, 2);
            day = time.substr(6, 2);
            hour = time.substr(8, 2);
            minute = time.substr(10, 2);
        }
        dstr = year + "-" + month + "-" + day + " " + hour + ":" + minute;
        return dstr;
    }
}


Vue.prototype.toCommonTimeAlbum = function (time) {
    if (time != null || time != "" || typeof (time) != "undefined") {
        let year;
        let month;
        let day;
        let hour;
        let minute;
        let timeStr;
        if (/^\d{10}$/.test(time)) {
            // int 时间戳类型
            var date = new Date(time * 1000);
            month = date.getMonth() + 1;
            day = date.getDate();
            hour = date.getHours();
            minute = date.getMinutes();
            year = date.getFullYear();

            if (month < 10) month = "0" + month;
            if (day < 10) day = "0" + day;
            if (hour < 10) hour = "0" + hour;
            if (minute < 10) minute = "0" + minute;
        } else if (/^[0-9]{14}$/.test(time)) {
            // string 日期时间类型
            // console.log(time);
            year = time.substr(0, 4);
            month = time.substr(4, 2);
            day = time.substr(6, 2);
            hour = time.substr(8, 2);
            minute = time.substr(10, 2);
        }

        if (year == new Date().getFullYear()) {
            timeStr = month + "-" + day;
        } else {
            timeStr = year + "-" + month + "-" + day;
        }
        return timeStr;
    }
};
Vue.prototype.simpleNum = function (num) {
    let snum = num;
    if (Math.abs(num) > 100000) {
        snum = (num / 10000).toFixed(2) + "万"
    }
    return snum;
}
Vue.prototype.getVideoTime = function (seconds) {
    seconds = parseInt(seconds);
    let hours;
    let minutes;
    let timeStr = "";

    if (seconds < 3600) {
        if (seconds < 60) {
            minutes = "00";
        } else {
            minutes = Math.floor(seconds / 60);
            if (minutes < 10) minutes = "0" + minutes;
            seconds = seconds % 60;
        }
        if (seconds < 10) seconds = "0" + seconds;
        timeStr = minutes + ":" + seconds;
    } else {
        // 小时
        hours = Math.floor(seconds / 3600);
        if (hours < 10) hours = "0" + hours;
        seconds = seconds % 3600;
        minutes = Math.floor(seconds / 60);
        if (minutes < 10) minutes = "0" + minutes;
        seconds = seconds % 60;
        if (seconds < 10) seconds = "0" + seconds;
        timeStr = hours + ":" + minutes + ":" + seconds;
    }

    // console.log(timeStr);
    return timeStr;
};

// 获取当前时间（yyyymmddHHmmss格式）
Vue.prototype.toFullTime = function () {
    let date = new Date();
    let year = date.getFullYear();
    year = year.toString();
    let month = date.getMonth() + 1;
    if (month < 10)
        month = "0" + month;
    let day = date.getDate();
    if (day < 10)
        day = "0" + day;
    let hour = date.getHours();
    if (hour < 10)
        hour = "0" + hour;
    let minute = date.getMinutes();
    if (minute < 10)
        minute = "0" + minute;
    let second = date.getSeconds();
    if (second < 10)
        second = "0" + second;
    return year + month + day + hour + minute + second;
};

// main.js里面的代码
router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面meta */
    if (to.meta.content) {
        let head = document.getElementsByTagName("head");
        let meta = document.createElement("meta");
        meta.content = to.meta.content;
        meta.name = "keywords";
        // console.log(meta);
        head[0].appendChild(meta);
    }
    /* 路由发生变化修改页面title */
    // document.title = to.meta.title;
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    next();
});

// 生成页面的meta信息
Vue.prototype.createMetaInfo = function (metaInfo) {
    let head = document.getElementsByTagName("head");
    let meta1 = document.createElement("meta");
    let meta2 = document.createElement("meta");
    if (metaInfo.content) {
        meta1.content = metaInfo.content;
        meta1.name = "description";
    }
    if (metaInfo.name) {
        meta2.name = "name";
        meta2.content = metaInfo.name;
        meta2.setAttribute("itemprop", "name");
    }
    // console.log(meta);
    head[0].appendChild(meta1);
    head[0].appendChild(meta2)
    // 设置标题
    if (metaInfo.title) {
        document.title = metaInfo.title;
    }
};

Vue.prototype.jsonParse = function (jsonStr) {
    return JSON.parse(jsonStr);
};
Vue.prototype.jsonParsehurl = function (jsonStr) {
    try {
        return JSON.parse(jsonStr).hdurl;
    } catch (e) {
        return jsonStr;
    }

};

Vue.prototype.getFirstUrl = function (urls) {
    if (urls != null && urls != "" && typeof (urls) != "undefined") {
        let urlarr = urls.split(",");
        return urlarr[0];
    } else {
        return "";
    }

};

Vue.prototype.getHlsUrl = function (urls) {
    let m3 = "";
    if (urls != null && urls != "" && typeof (urls) != "undefined") {
        var u = navigator.userAgent, app = navigator.appVersion;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        let urlarr = urls.split(",");
        let alen = urlarr.length;
            for (let i = 0; i < alen; i++) {
                if (urlarr[i].indexOf(".m3u8")!= -1 || urlarr[i].indexOf(".M3U8")!= -1) {
                    m3 = urlarr[i];
                    break;
                }
            }

        if(m3==""){
            m3=urlarr[0];
        }

        if(m3.indexOf("yztv.wshls")!=-1){
            m3=m3.replace("yztv.wshls","yztv-wshls");
        }
        if(m3.indexOf("yztv.live")!=-1){
            m3= m3.replace("yztv.live","yztv-wshls");
        }
        if(m3.indexOf("yztv-live")!=-1){
            m3= m3.replace("yztv-live","yztv-wshls");
        }
        if(IsPC()){
            m3= m3.replace("https:","").replace("http:","");
        }
    }

//$("#democon").html(m3);
    return m3;
}

//获取url参数的方法 url:url字符串链接;num:从后往前数以‘/’分割的位置。
// 如 http://www.demo.com/sss/ddd/{频道}/aaa?222=222 则如果想取{频道}的值则传入
//url:http://www.demo.com/sss/ddd/{频道}/aaa?222=222
//num:2
Vue.prototype.getParma = function (url, num) {
    url = url.split("?")[0];
    let urlarry = url.split("/");
    let len = urlarry.length;
    let result = urlarry[len - num];
    if (result == null || result == "" || typeof (result) == "undefined") {
        result = "";
    }
    return result;
};

// 抓取URL参数的方法
Vue.prototype.getUrlParam = function (name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    let r = window.location.search.substr(1).match(reg);      //匹配目标参数
    if (r != null) {
        return unescape(r[2]);
    }
    return null;    //返回参数值
};

// 判断一个对象是否为空
Vue.prototype.isEmptyObject = function (obj) {
    let arr = Object.keys(obj);
    return arr.length == 0;
};

// 关闭遮罩层的方法
Vue.prototype.closeLayer = function () {

};

//刷新title
Vue.prototype.freshTitles = function (title) {
    let body = document.getElementsByTagName('body')[0];
    document.title = title;
    let iframe = document.createElement("iframe");
    iframe.style.display = "none";
    iframe.setAttribute("src", "./assets/images/yf.png");
    let d = function () {
        setTimeout(function () {
            iframe.removeEventListener('load', d);
            document.body.removeChild(iframe);
        }, 0);
    };
    iframe.addEventListener('load', d);
    document.body.appendChild(iframe);
};



//设置cookie
Vue.prototype.SetCookie=function (cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    console.info(cname + "=" + encodeURI(cvalue) + "; " + expires);
    document.cookie = cname + "=" + encodeURI(cvalue) + ";path=/;" + expires;
    console.info(document.cookie);
},
//获取cookie
Vue.prototype.GetCookie=function (cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return decodeURI(c.substring(name.length, c.length));
    }
    return "";
}

//清除cookie
Vue.prototype.ClearCookie= function (u) {
    this.SetCookie(u, "", -1);

}
Vue.prototype.getStringJsonVal=function(data,key){
    let jsonObj=JSON.parse(data);
    let result=eval('jsonObj.'+key);
    return result;
}
Vue.prototype.getStringJsonValLiveroom=function(data){
    let jsonObj=JSON.parse(data);
    let result="";
    if(jsonObj.identify!=""&&jsonObj.identify!=null&&typeof (jsonObj.identify)!="undefined"){
        result=jsonObj.identify+"-";
    }else if(jsonObj.identity!=""&&jsonObj.identity!=null&&typeof (jsonObj.identity)!="undefined"){
        result=jsonObj.identity+"-";
    }
    return result;
}
/**判断屏幕是否全面屏 */
Vue.prototype.judgeBigScreen=function(){
  //，这里根据返回值 true 或false ,返回true的话 则为全面屏
    let result = false;
    const rate = window.screen.height / window.screen.width;
    let limit =  window.screen.height == window.screen.availHeight ? 1.8 : 1.65; // 临界判断值

    // window.screen.height为屏幕高度
    //  window.screen.availHeight 为浏览器 可用高度

    if (rate > limit) {
        result = true;
    }
    return result;
};

//是否自己添加图片压缩格式
Vue.prototype.addCmdUrl=function(url,width,quality){
if(url.indexOf("?")==-1){
    url=url+"?cmd=imageprocess/format/jpg/processtype/2/width/"+width+"/quality/"+quality;
}
return url;
}
// 创建和挂载根实例：通过router配置参数注入全局路由
new Vue({
    router,
    render: h => h(App)
}).$mount('#app');